<template>
	<view :class="[primarySize()]">
		<view class="container">
			<hx-navbar ref="hxnb" :config="config" v-if="primarySize()"></hx-navbar>
			<hx-navbar ref="hxnb" :config="configo" v-else></hx-navbar>
			<view class="plr30 ptb20">
				
				<view class="item flex-box" v-if="system.message" @click="bindGoSystem">
					<view class="cover">
						<text class="dot" v-if="system.total>0"></text>
						<image src="@/static/public/icon41.png" mode="aspectFill"></image>
					</view>
					<view class="flex-grow-1 plr20">
						<view class="flex-box">
							<view class="m-ellipsis flex-grow-1 pr30 fwb fs30 col1 lh42">系统通知</view>
							<view class="cola fs24 ">{{formatDatetime(system.message.createtime,'yyyy-mm-dd hh:ii')}}</view>
						</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis">{{system.message.name}}</view>
					</view>
				</view>
				<view class="item flex-box" v-if="personal.message" @click="bindGoPersonal">
					<view class="cover">
						<text class="dot" v-if="personal.total>0"></text>
						<image src="@/static/public/icon42.png" mode="aspectFill"></image>
					</view>
					<view class="flex-grow-1 plr20">
						<view class="flex-box">
							<view class="m-ellipsis flex-grow-1 pr30 fwb fs30 col1 lh42">个人通知</view>
							<view class="cola fs24 ">{{personal.message.createtime}}</view>
						</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis">{{personal.message.title}}</view>
					</view>
				</view>
				<view class="item flex-box" v-if="activity.message" @click="bindGoActivity">
					<view class="cover">
						<text class="dot" v-if="activity.total>0"></text>
						<image src="@/static/public/icon43.png" mode="aspectFill"></image>
					</view>
					<view class="flex-grow-1 plr20">
						<view class="flex-box">
							<view class="m-ellipsis flex-grow-1 pr30 fwb fs30 col1 lh42">活动通知</view>
							<view class="cola fs24 ">{{formatDatetime(activity.message.createtime)}}</view>
						</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis">{{system.message.name}}</view>
					</view>
				</view>
				<!-- <view class="item flex-box">
					<view class="cover">
						<text class="dot"></text>
						<image src="@/static/public/icon44.png" mode="aspectFill"></image>
					</view>
					<view class="flex-grow-1 plr20">
						<view class="flex-box">
							<view class="m-ellipsis flex-grow-1 pr30 fwb fs30 col1 lh42">认证审核通知</view>
							<view class="cola fs24 ">04.20 21:21</view>
						</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis">认证审核通知</view>
					</view>
				</view> -->
							
				
				<view class="item flex-box" v-for="(item,index) in sessionList" @click="bindChat" :data-index="index">
					<view class="cover">
						<text class="dot" v-if="item.unread_num>0"></text>
						<image :src="userinfo.id!=item.receive_user_id?item.receiveuser.avatar:item.senduser.avatar" mode="aspectFill"></image>
					</view>
					<view class="flex-grow-1 plr20">
						<view class="flex-box">
							<view class="m-ellipsis flex-grow-1 pr30 fwb fs30 col1 lh42">{{userinfo.id!=item.receive_user_id?item.receiveuser.nickname:item.senduser.nickname }}</view>
							<view class="cola fs24 ">{{item.newmsg.createtime_text}}</view>
						</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis" v-if="item.newmsg.type==7">【面试邀请】</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis" v-else-if="item.newmsg.type==6">【人才】</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis" v-else-if="item.newmsg.type==4">【投递咨询】</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis" v-else-if="item.newmsg.type==3">【职位咨询】</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis" v-else-if="item.newmsg.type==2">【图片】</view>
						<view class="mt15 fs30 col5 lh42 m-ellipsis" v-else-if="item.newmsg.type==1">{{item.newmsg.message}}</view>
					</view>
				</view>
							
				
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				
				config: {
					color: '#101010',
					back: false,
					title: '消息',
					//背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
					backgroundColor: [1, ['#2A79F2', '#2A79F2']],
					statusBarFontColor: ['#ffffff', '#ffffff']
				},
				configo: {
					color: '#ffffff',
					back: true,
					title: '消息',
					//背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
					backgroundColor: [1, ['#ff7100', '#ff7100']],
					statusBarFontColor: ['#ffffff', '#ffffff']
				},
				userinfo: null,
				megNum: 0,
				personal: {total:0,message: null},
				activity: {total:0,message: null},
				system: {total:0,message: null},
				sessionList: [],
				sessionListMore: {
					page: 1,
					pagesize: 50,
					text: '加载中...'
				},
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.primaryColor()
			})
		},
		/**
		* 生命周期函数--监听页面加载
		*/
		onLoad() {
			
		    let page = this;
		    this.userinfo = this.$core.getUserinfo();
		    this.refresh();
		    //获取列表成功回调
		    uni.$on("session_list_callback", function (data) {
		      let curPageData = data.data;
		      page.sessionList = curPageData;
		      page.sessionListMore = {
		        page: 1,
		        pagesize: 50,
		        text: ''
		      };
			})
			uni.$on(app.globalData.Events.UserLogin, this.eventOnUserLogin, this);
		    uni.$on(app.globalData.Events.UserLogout, this.eventOnUserLogout, this);
			uni.$on(app.globalData.Events.UserinfoChange, this.eventOnUserinfoChange, this);
			//未读消息
			if (app.globalData.chat_config.SocketTask) {
				page.$chatIM.setMessageBadge(false)
			}
		    uni.$on(app.globalData.Events.WebsocketMessageCount, this.eventWebsocketMessageCount, this);
		    //刷新列表
		    uni.$on('refresh_session', function () {
		      page.refresh();
			})
		},
		onUnload: function () {
			uni.$off(app.globalData.Events.UserLogin, this.eventOnUserLogin);
			uni.$off(app.globalData.Events.UserLogout, this.eventOnUserLogout);
			uni.$off(app.globalData.Events.UserinfoChange, this.eventOnUserinfoUpdate);
		    uni.$off(app.globalData.Events.WebsocketMessageCount, this.eventWebsocketMessageCount);
		    uni.$off('session_list_callback');
		    uni.$off('refresh_session');
		},
		onPullDownRefresh() {
			this.refresh();
		},
		methods: {
			//刷新
			refresh() {
				this.$core.get({
					url: 'xilujob.message/summary',
					loading: false,
					success: ret => {
				        this.personal = ret.data.personal;
				        this.activity = ret.data.activity;
				        this.system = ret.data.system;
					}
				})
				this.sessionListMore = {
					page: 1,
					pagesize: 50,
					text: '加载中...'
				},
			    this.sessionLists();
				uni.stopPullDownRefresh();
			},
			bindGoPersonal() {
			    let total = this.personal.total;
			    if(total>0) {
			      this.personal.total = 0;
			      uni.$emit(app.globalData.Events.ClearMessageCount, total);
			    }
			    uni.navigateTo({url: '/pages/public/notice_list?type=personal'});
			},
			bindGoActivity() {
			    let total = this.activity.total;
			    if(total>0) {
			      this.activity.total = 0;
			      uni.$emit(app.globalData.Events.ClearMessageCount, total);
			    }
			    uni.navigateTo({url: '/pages/public/notice_list?type=activity'});
			},
			bindGoSystem() {
			    let total = this.system.total;
			    if(total>0) {
			      this.system.total = 0;
			      uni.$emit(app.globalData.Events.ClearMessageCount, total);
			    }
			    uni.navigateTo({url: '/pages/public/notice_list?type=system'});
			},
			//聊天列表
			sessionLists() {
			    let sessionListMore = this.sessionListMore;
			    this.$chatIM.wsSend({
			      type: 'sessionlist',
			      page: sessionListMore.page,
			      pagesize: sessionListMore.pagesize
			    });
			    uni.stopPullDownRefresh();
			},
			//前往聊天室
			bindChat(event) {
				let index = event.currentTarget.dataset.index;
				let item = this.sessionList[index];
				let receive_user_id = this.userinfo.id != item.receive_user_id ? item.receiveuser.id : item.senduser.id;
				let to_username = this.userinfo.id != item.receive_user_id ? item.receiveuser.nickname : item.senduser.nickname;
				let chat = {
					company_id: item.company_id,
					resume_id: item.resume_id,
					receive_user_id: receive_user_id,
					to_username: to_username
				}
				chat.refresh_status = item.unread_num>0?1:0;
				uni.navigateTo({
					url: '/pages/public/chat?param='+JSON.stringify(chat),
					events:{
						refreshSession:data=>{
							this.$chatIM.setMessageBadge(false)
						}
					},
					// success(res) {
					// 	res.eventChannel.emit('chatTransfor', chat)
					// }
				})
			},
			
			eventOnUserLogin(userinfo) {
				this.userinfo = userinfo
			    this.refresh();
			},
			eventOnUserLogout() {
			    //this.refresh();
			},
			eventOnUserinfoChange: function(){
				this.refresh();
			},
			//未读消息
			eventWebsocketMessageCount(data) {
				this.megNum = this.userinfo ? app.globalData.chat_config.messageCount : 0;
				// this.$forceUpdate()
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		width: 690rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(0, 0, 0, 0.03);
		border-radius: 18rpx;
		margin-bottom: 20rpx;
		padding: 30rpx;

		.cover {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			position: relative;

			image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				display: block;
			}

			.dot {
				position: absolute;
				width: 21rpx;
				height: 21rpx;
				background: #F91E1E;
				border: 2rpx solid #FFFFFF;
				border-radius: 50%;
				top: -2rpx;
				right: 12rpx;
				z-index: 2;
			}
		}
	}
</style>